<?php

namespace App\Repositories\Config;


use App\Handlers\role;
use App\Http\Controllers\Admin\BaseController;
use App\Models\Config\Channels;
use App\Models\Enum\Status;
use App\Models\GameTool\GameMail;
use App\Repositories\BaseRepository;

class ChannelsRepository extends BaseRepository
{
    public function __construct(Channels $model)
    {
        $this->model = $model;
    }

    //获取分页后的    所有渠道
    public function getPage($request)
    {
        $pageSize = 10;
        $query = Channels::query();
        if (!empty($request->get('game_id'))) {
            $query->where(['game_id' => $request->get('game_id')]);
        }
        if (!empty($request->get('page_size'))) {
            $pageSize = $request->get('page_size');
        }
        return $query->latest('id')->paginate($pageSize);
    }

    /**
     * 获取全部的 渠道信息
     */
    public function getAll()
    {
        return Channels::query()
            ->with('game')
            ->where(['status' => Status::NORMAL])
            ->orderBy('game_id', 'DESC')
            ->orderBy('channel_id', 'ASC')
            ->get()
            ->toArray();
    }

    /**
     * 获取全部的渠道信息
     */
    public function getChannels($condition)
    {
        $query = Channels::query();
        if (isset($condition['status'])) {
            $query->where('status', '=', $condition['status']);
        }
        return $query->orderBy('updated_at', 'DESC')->get()->toArray();
    }

    /**
     * 所有有权限的渠道
     */
    public function getAllAuth()
    {
        $game_id = session('game_id', '');

        $channelsAuth = role::getChannelsAuth();//有权限的渠道
        return Channels::query()->with('game')
            ->where(['game_id' => $game_id])
            ->where(['status' => Status::NORMAL])
            ->whereIn('id', $channelsAuth)
            ->orderBy('channel_id', 'DESC')
            ->get()
            ->toArray();
    }

    public function ById($id)
    {
        return Channels::find($id);
    }

    public function getByChannelId($channelId)
    {
        return Channels::query()
            ->where('game_id', session('game_id'))
            ->where('channel_id', $channelId)->first();
    }

    //获取当前游戏的所有渠道
    public function getCurGameChannels()
    {
        return Channels::query()
            ->where('game_id', session('game_id'))
            ->get();
    }
}